package com.ruoyi.eqmonitor.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 水文站接口工具类
 */
@Slf4j
public class HydrologicStationUtil {

    static String pathUrl =  "http://dsjweb.shui00.com/SkData/api/RainWater/v1/";

    static String apiKEY = "1Z37o1vG4Ep8gwQOH8TKCycq10q7nfU7";

    public static String doGet(String url, Map<String, String> param) {
        url = url + "?apikey=" + apiKEY;
        if (param.size() > 0) {
            StringBuilder urlBuilder = new StringBuilder(url);
            for (String key : param.keySet()) {
                String s = param.get(key);
                try {
                    s = URLEncoder.encode(s, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    log.error("转码失败",e);
                }
                urlBuilder.append("&").append(key).append("=").append(s);
            }
            url = urlBuilder.toString();
        }
        return HttpClientsUtil.doGet(pathUrl + url);
    }

    public static String doGetByIds(String url, Map<String, String> param,String[] ids) {
        url = url + "?apikey=" + apiKEY;
        if (param.size() > 0) {
            StringBuilder urlBuilder = new StringBuilder(url);
            for (String key : param.keySet()) {
                String s = param.get(key);
                try {
                    s = URLEncoder.encode(s, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    log.error("转码失败",e);
                }
                urlBuilder.append("&").append(key).append("=").append(s);
            }
            for (String id : ids){
                urlBuilder.append("&id=").append(id);
            }
            url = urlBuilder.toString();
        }
        return HttpClientsUtil.doGet(pathUrl + url);
    }

}
